/*
* Copyright(c) 2021 YCHS. All rights Reserved
*/
package com.yckj.web.uol.util;

import java.io.IOException;
import java.io.InputStream;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

/**
*
*
*@author xudingying
*@version 1.0 2021-7-12
*/
public class MyBatisUtil {
	private static SqlSessionFactory  sqlSessionFactory = null;
	private static InputStream is = null;
	static {
		// 1.加载MyBatis的全局配置文件
				try {
					is = Resources.getResourceAsStream("mybatis-config.xml");
					// 2.创建SqlSessionFactory
					sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}finally {
					if(is!= null) {
						try {
							is.close();
						} catch (IOException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}
				}
				
			}
	/**
	 * 获得SqlSession方法
	 * @return
	 */
public static SqlSession getSession() {
	SqlSession session = sqlSessionFactory.openSession();
	return session;
}
/**
 * 关闭连接
 * @param session
 */
public static void close(SqlSession session) {
	if(session != null) {
		session.close();
	}
} 
}
